home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / sortaddr < prev    next >
Encoding:
AWK Script  |  1997-08-26  |  3.9 KB  |  142 lines

  1. #!/usr/local/bin/gawk -f
  2. #!/usr/bin/awk -f
  3. # @(#) sortaddr.gawk 1.0 97/01/06
  4. # sortaddr: sort address file(s) containing records separated by
  5. # lines consisting solely of '+'.
  6. # Records are sorted based on the lexical value of the entire record.
  7. # 92/08/05 john h. dubois iii (john@armory.com)
  8.  
  9. BEGIN {
  10.     RS = "+"
  11.     i = 0
  12. }
  13.  
  14. {
  15.     if ($0 != "\n" && $0 != "")
  16.     Elem[++i] = $0
  17. }
  18.  
  19. END {
  20.     qsortNumIndByValue(Elem,1,i)
  21.     printf "+"
  22.     for (j = 1; j <= i; j++)
  23.     printf "%s+",Elem[j]
  24.     print ""
  25. }
  26.  
  27. ### Begin qsort routines
  28.  
  29. # Arr[] is an array of values with arbitrary indices.
  30. # k[] is returned with numeric indices 1..n.
  31. # The values in k[] are the indices of Arr[],
  32. # ordered so that if Arr[] is stepped through
  33. # in the order Arr[k[1]] .. Arr[k[n]], it will be stepped
  34. # through in order of the values of its elements.
  35. # The return value is the number of elements in the arrays (n).
  36. function qsortArbIndByValue(Arr,k,  ArrInd,ElNum) {
  37.     ElNum = 0
  38.     for (ArrInd in Arr)
  39.     k[++ElNum] = ArrInd
  40.     qsortSegment(Arr,k,1,ElNum)
  41.     return ElNum
  42. }
  43.  
  44. # Sort a segment of an array.
  45. # Arr[] contains data with arbitrary indices.
  46. # k[] has indices 1..nelem, with the indices of arr[] as values.
  47. # This function sorts the elements of arr that are pointed to by
  48. # k[start..end], swapping the values of elements of k[] so that
  49. # when this function returns arr[k[start..end]] will be in order.
  50. function qsortSegment(Arr,k,start,end,  left,right,sepval,tmp,tmpe,tmps) {
  51.     # handle two-element case explicitly for a tiny speedup
  52.     if ((end - start) == 1) {
  53.     if (Arr[tmps = k[start]] > Arr[tmpe = k[end]]) {
  54.         k[start] = tmpe
  55.         k[end] = tmps
  56.     }
  57.     return
  58.     }
  59.     # Make sure comparisons act on these as numbers
  60.     left = start+0
  61.     right = end+0
  62.     sepval = Arr[k[int((left + right) / 2)]]
  63.     # Make every element <= sepval be to the left of every element > sepval
  64.     while (left < right) {
  65.     while (Arr[k[left]] < sepval)
  66.         left++
  67.     while (Arr[k[right]] > sepval)
  68.         right--
  69.     if (left < right) {
  70.         tmp = k[left]
  71.         k[left++] = k[right]
  72.         k[right--] = tmp
  73.     }
  74.     }
  75.     if (left == right)
  76.     if (Arr[k[left]] < sepval)
  77.         left++
  78.     else
  79.         right--
  80.     if (start < right)
  81.     qsortSegment(Arr,k,start,right)
  82.     if (left < end)
  83.     qsortSegment(Arr,k,left,end)
  84. }
  85.  
  86. # Arr[] is an array of values with arbitrary indices.
  87. # k[] is returned with numeric indices 1..n.
  88. # The values in k are the indices of Arr[],
  89. # ordered so that if Arr[] is stepped through
  90. # in the order Arr[k[1]] .. Arr[k[n]], it will be stepped
  91. # through in order of the values of its indices.
  92. # The return value is the number of elements in the arrays (n).
  93. # If the indexes are numeric, Numeric should be true, so that they can be
  94. # compared as such rather than as strings.  Numeric indexes do not have to be
  95. # contiguous.
  96. function qsortByArbIndex(Arr,k,Numeric,  ArrInd,ElNum) {
  97.     ElNum = 0
  98.     if (Numeric)
  99.     # Indexes do not preserve numeric type, so must be forced
  100.     for (ArrInd in Arr)
  101.         k[++ElNum] = ArrInd+0
  102.     else
  103.     for (ArrInd in Arr)
  104.         k[++ElNum] = ArrInd
  105.     qsortNumIndByValue(k,1,ElNum)
  106.     return ElNum
  107. }
  108.  
  109. # Arr is an array of elements with contiguous numeric indexes to be sorted
  110. # by value.
  111. # start and end are the starting and ending indexes of the range to be sorted.
  112. function qsortNumIndByValue(Arr,start,end,  left,right,sepval,tmp,tmpe,tmps) {
  113.     # handle two-element case explicitly for a tiny speedup
  114.     if ((start - end) == 1) {
  115.     if ((tmps = Arr[start]) > (tmpe = Arr[end])) {
  116.         Arr[start] = tmpe
  117.         Arr[end] = tmps
  118.     }
  119.     return
  120.     }
  121.     left = start+0
  122.     right = end+0
  123.     sepval = Arr[int((left + right) / 2)]
  124.     while (left < right) {
  125.     while (Arr[left] < sepval)
  126.         left++
  127.     while (Arr[right] > sepval)
  128.         right--
  129.     if (left <= right) {
  130.         tmp = Arr[left]
  131.         Arr[left++] = Arr[right]
  132.         Arr[right--] = tmp
  133.     }
  134.     }
  135.     if (start < right)
  136.     qsortNumIndByValue(Arr,start,right)
  137.     if (left < end)
  138.     qsortNumIndByValue(Arr,left,end)
  139. }
  140.  
  141. ### End qsort routines
  142.